package com.amigo.online.provider;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import com.amigo.online.provider.compare.quartzjob.full.MusicInfoComparisonJob;
import com.amigo.online.provider.compare.quartzjob.full.UserInfoComparisonJob;
import com.amigo.online.provider.compare.quartzjob.full.VideoInfoComparisonJob;
import com.amigo.online.provider.compare.quartzjob.random.MusicInfoRandomComparisonJob;
import com.amigo.online.provider.compare.quartzjob.random.UserInfoRandomComparisonJob;
import com.amigo.online.provider.compare.quartzjob.random.VideoInfoRandomComparisonJob;

@Configuration
@Component
public class QuartzJob {

    /**
     * job 全量比较组名
     */
    private static final String JOB_FULL_COMPARISON = "JobFull";

    /**
     * job 随机比较组名
     */
    private static final String JOB_RANDOM_COMPARISON = "JobRandom";

    /**
     * trigger 全量比较组名
     */
    private static final String TRIGGER_FULL_COMPARISON = "TriggerFull";

    /**
     * trigger 随机比较组名
     */
    private static final String TRIGGER_RANDOM_COMPARISON = "TriggerRandom";

    @Bean
    public JobDetail userCompareFullJob() {
        return JobBuilder.newJob(UserInfoComparisonJob.class)
                .withIdentity("UserInfoFullComparisonJob", JOB_FULL_COMPARISON).storeDurably().build();
    }

    @Bean
    public Trigger userCompareFullTrigger() {
        return TriggerBuilder.newTrigger().forJob(userCompareFullJob())
                .withIdentity("UserInfoFullComparisonTrigger", TRIGGER_FULL_COMPARISON)
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0 2 * * ?")).build();
    }

    @Bean
    public JobDetail videoCompareFullJob() {
        return JobBuilder.newJob(VideoInfoComparisonJob.class)
                .withIdentity("VideoInfoFullComparisonJob", JOB_FULL_COMPARISON).storeDurably().build();
    }

    @Bean
    public Trigger videoCompareFullTrigger() {
        return TriggerBuilder.newTrigger().forJob(videoCompareFullJob())
                .withIdentity("VideoInfoFullComparisonTrigger", TRIGGER_FULL_COMPARISON)
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0 3 * * ?")).build();
    }

    @Bean
    public JobDetail musicCompareFullJob() {
        return JobBuilder.newJob(MusicInfoComparisonJob.class)
                .withIdentity("MusicInfoFullComparisonJob", JOB_FULL_COMPARISON).storeDurably().build();
    }

    @Bean
    public Trigger musicCompareFullTrigger() {
        return TriggerBuilder.newTrigger().forJob(musicCompareFullJob())
                .withIdentity("VideoInfoFullComparisonTrigger", TRIGGER_FULL_COMPARISON)
                .withSchedule(CronScheduleBuilder.cronSchedule("0 30 2 * * ?")).build();
    }

    @Bean
    public JobDetail userCompareRandomJob() {
        return JobBuilder.newJob(UserInfoRandomComparisonJob.class)
                .withIdentity("UserInfoRandomComparisonJob", JOB_RANDOM_COMPARISON).storeDurably().build();
    }

    @Bean
    public Trigger userCompareRandomTrigger() {
        return TriggerBuilder.newTrigger().forJob(userCompareRandomJob())
                .withIdentity("UserInfoRandomComparisonTrigger", TRIGGER_RANDOM_COMPARISON)
                .withSchedule(CronScheduleBuilder.cronSchedule("*/10 * * * * ?")).build();
    }

    @Bean
    public JobDetail videoCompareRandomJob() {
        return JobBuilder.newJob(VideoInfoRandomComparisonJob.class)
                .withIdentity("VideoInfoRandomComparisonJob", JOB_RANDOM_COMPARISON).storeDurably().build();
    }

    @Bean
    public Trigger videoCompareRandomTrigger() {
        return TriggerBuilder.newTrigger().forJob(videoCompareRandomJob())
                .withIdentity("VideoInfoRandomComparisonTrigger", TRIGGER_RANDOM_COMPARISON)
                .withSchedule(CronScheduleBuilder.cronSchedule("*/10 * * * * ?")).build();
    }

    @Bean
    public JobDetail musicCompareRandomJob() {
        return JobBuilder.newJob(MusicInfoRandomComparisonJob.class)
                .withIdentity("MusicInfoRandomComparisonJob", JOB_RANDOM_COMPARISON).storeDurably().build();
    }

    @Bean
    public Trigger musicCompareRandomTrigger() {
        // 每隔五秒执行一次
		/*SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5)
				.repeatForever();*/
        return TriggerBuilder.newTrigger().forJob(musicCompareRandomJob())
                .withIdentity("MusicInfoRandomComparisonTrigger", TRIGGER_RANDOM_COMPARISON)
                .withSchedule(CronScheduleBuilder.cronSchedule("*/10 * * * * ?")).build();
    }

    @Bean
    public JobDetail goodsCompareRandomJob() {
        return JobBuilder.newJob(MusicInfoRandomComparisonJob.class)
                .withIdentity("GoodsInfoRandomComparisonJob", JOB_RANDOM_COMPARISON).storeDurably().build();
    }

    @Bean
    public Trigger goodsCompareRandomTrigger() {
        return TriggerBuilder.newTrigger().forJob(musicCompareRandomJob())
                .withIdentity("GoodsInfoRandomComparisonTrigger", TRIGGER_RANDOM_COMPARISON)
                .withSchedule(CronScheduleBuilder.cronSchedule("*/10 * * * * ?")).build();
    }

    @Bean
    public JobDetail merchantCompareRandomJob() {
        return JobBuilder.newJob(MusicInfoRandomComparisonJob.class)
                .withIdentity("MerchantInfoRandomComparisonJob", JOB_RANDOM_COMPARISON).storeDurably().build();
    }

    @Bean
    public Trigger merchantCompareRandomTrigger() {
        return TriggerBuilder.newTrigger().forJob(musicCompareRandomJob())
                .withIdentity("MerchantInfoRandomComparisonTrigger", TRIGGER_RANDOM_COMPARISON)
                .withSchedule(CronScheduleBuilder.cronSchedule("*/10 * * * * ?")).build();
    }
}
